package com.group.eams.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.eams.pojo.dto.SoftwareCategoryQueryDto;
import com.group.eams.pojo.entity.SoftwareCategory;
import com.group.eams.service.SoftwareCategoryService;
import com.group.pojo.dto.BatchDeleteDto;
import com.group.result.Result;
import com.group.result.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 软件分类表(SoftwareCategory)表控制层
 *
 * @author DHUser
 * @since 2024-10-30 12:58:29
 */
@RestController
@RequestMapping("/softwareCategory")
public class SoftwareCategoryController  {
    /**
     * 服务对象
     */
    @Autowired
    private SoftwareCategoryService softwareCategoryService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping(value = "/find",method = RequestMethod.GET)
    public Result findById(@RequestParam Long id) {
        SoftwareCategory softwareCategory = softwareCategoryService.getById(id);
        return Result.buildSuccess(softwareCategory);
    }

    /**
     * 分页查询
     * @param softwareCategoryQueryDto
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result list(SoftwareCategoryQueryDto softwareCategoryQueryDto) {
        Page<SoftwareCategory> page= softwareCategoryService.findByQueryDto(softwareCategoryQueryDto);
        return Result.buildSuccess(page.getTotal(),page.getRecords());
    }

    /**
     * 查询全部
     * @return
     */
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Result findAll(){
        List<SoftwareCategory> softwareCategoryList = softwareCategoryService.list();
        return Result.buildSuccess(softwareCategoryList);
    }

    /**
     * 添加
     * @param softwareCategory
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody SoftwareCategory softwareCategory){
        boolean result = softwareCategoryService.save(softwareCategory);
        return result ? Result.buildSuccess() : Result .buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 批量添加
     * @param softwareCategory
     * @return
     */
    @RequestMapping(value = "/addBatch",method = RequestMethod.POST)
    public Result addBatch(@RequestBody List<SoftwareCategory> softwareCategory){
        boolean result = softwareCategoryService.saveBatch(softwareCategory);
        return result ? Result.buildSuccess() : Result .buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 修改
     * @param softwareCategory
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody SoftwareCategory softwareCategory){
        boolean result = softwareCategoryService.updateById(softwareCategory);
        return result ? Result.buildSuccess() : Result .buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 批量修改
     * @param softwareCategory
     * @return
     */
    @RequestMapping(value = "/updateBatch",method = RequestMethod.POST)
    public Result updateBatch(@RequestBody List<SoftwareCategory> softwareCategory){
        boolean result = softwareCategoryService.updateBatchById(softwareCategory);
        return result ? Result.buildSuccess() : Result .buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Result delete(@RequestParam Long id){
        boolean result = softwareCategoryService.removeById(id);
        return result ? Result.buildSuccess() : Result .buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 批量删除
     * @param batchDeleteDto
     */
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
    public Result deleteBatch(@RequestBody BatchDeleteDto batchDeleteDto){
        boolean result = softwareCategoryService.removeByIds(batchDeleteDto.getIds());
        return result ? Result.buildSuccess() : Result .buildFail(ResultEnum.OPTION_FAIL);
    }
}

